﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using MSBTC.Model;
using MSBTC.CrystalReport;

namespace MSBTC.ReportViewer
{
    public partial class PMSRptForm : Form
    {
        PMSContext dbcontext = new PMSContext();
        FormHelpers help = new FormHelpers();
        PMSAll pms = new PMSAll();
       
        public PMSRptForm()
        {
            InitializeComponent();
        }

        private void cryspPMS_Load(object sender, EventArgs e)
        {
            string name = help.Pname();

            var proj = (from p in dbcontext.Projects
                        where p.Project_Name == name
                        select p).SingleOrDefault();
            if (proj != null)
            {
                
                TextObject txtobj;
                txtobj = (TextObject)pms.ReportDefinition.ReportObjects["txtPname"];
                txtobj.Text = proj.Project_Name;

                var cname = dbcontext.Customers.Find(proj.Customerid_FK);
                txtobj = (TextObject)pms.ReportDefinition.ReportObjects["txtCname"];
                txtobj.Text = cname.Customer_Name;

                txtobj = (TextObject)pms.ReportDefinition.ReportObjects["txtStat"];
                txtobj.Text = proj.Status;

                //pms.SetDataSource(proj);
                
                var pjeq = (from pe in dbcontext.PrjEqs
                            join p in dbcontext.Projects on pe.Projectid_FK equals p.Projectid_PK
                            join eq in dbcontext.Equipments on pe.Equipid_FK equals eq.Equipmentid_PK
                            where p.Projectid_PK == proj.Projectid_PK && pe.Projectid_FK == proj.Projectid_PK
                            select new
                            {
                                eq.Equip_Name,
                                eq.Quantity,
                                eq.Brand,
                                pe.QuantityUsed
                            }).ToList();
                if (pjeq.Count != 0)
                {
                    EqPj rpt = new EqPj();
                    rpt.SetDataSource(pjeq);
                    rpt.Refresh();
                    //pms.Subreports["PMSEqp"].SetDataSource(pjeq);
                }

                var pjmat = (from m in dbcontext.MaterialsInventorys
                             join mt in dbcontext.PrjMatls on m.MaterialsInventoryid_PK equals mt.MaterialsInventoryid_FK
                             join p in dbcontext.Projects on mt.Projectid_FK equals p.Projectid_PK
                             where p.Projectid_PK == proj.Projectid_PK && mt.Projectid_FK == proj.Projectid_PK //&& mt.MaterialsInventoryid_PK == m.MaterialsInventoryid_FK
                             select new
                             {
                                 m.Material_Name,
                                 m.Unit,
                                 m.Brand_Name,
                                 m.Qty,
                                 mt.QtyUsed
                             }).ToList();
                if (pjmat.Count != 0)
                {
                    MatPj rpt = new MatPj();
                    rpt.SetDataSource(pjmat);
                    rpt.Refresh();
                    //pms.Subreports["PMSEqp"].SetDataSource(pjmat);
                }

                var lab = (from l in dbcontext.Labors
                           where l.Projectid_FK == proj.Projectid_PK
                           select l).ToList();
                if (lab.Count != 0)
                {
                    pms.Subreports["PMSLabor"].SetDataSource(lab);
                }

                //pms.SetDataSource(proj);
                pms.Refresh();
                cryspPMS.ReportSource = pms;
                cryspPMS.RefreshReport();
               
            }

        }

    }
}
